<template>
  <!-- 分包商分子公司汇总修改 -->
  <div class="e-form">
    <div class="tabs" v-if="Object.keys(basicFormData).length != 0">
      <el-tabs tab-position="left" v-model="tabsName" @tab-click="onChangeTab">
        <!-- 基本信息 -->
        <el-tab-pane
          label="基本信息"
          name="baseInfo"
          :disabled="clickTabFlag"
        ></el-tab-pane>
        <el-tab-pane
          label="分包商汇总明细"
          name="subcontractorCollect"
          :disabled="clickTabFlag"
        ></el-tab-pane>
        <el-tab-pane
          label="年度动态惩罚查询表"
          name="punishment"
          :disabled="clickTabFlag"
        ></el-tab-pane>
        <el-tab-pane
          label="评价情况统计表"
          name="orgCollect"
          :disabled="clickTabFlag"
        ></el-tab-pane>

        <el-tab-pane
          label="附件信息"
          name="accessory"
          :disabled="clickTabFlag"
        ></el-tab-pane>

        <div>
          <!-- 基本信息 -->
          <div id="baseInfoCon" class="con">
            <div class="e-form">
              <div class="tabs">
                <div class="tabs-title" id="baseInfo">基本信息</div>
                <div style="width: 100%" class="form">
                  <el-form :model="basicFormData" label-width="150px">
                    <el-row>
                       <BillTop title="分子公司汇总编辑" @cancel="handleClose"></BillTop>
                      <el-col :span="12">
                        <el-form-item label="汇总日期：">
                          <el-date-picker

                            v-model="basicFormData.billDate"
                            type="date"
                            placeholder="选择日期"
                            :clearable="false"
                          >
                          </el-date-picker>
                        </el-form-item>
                      </el-col>

                      <el-col :span="12">
                        <el-form-item label="汇总时间区：">
                          {{ basicFormData.summartDate }}
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-row>
                      <el-col :span="12">
                        <el-form-item label="评价机构："
                          >{{ basicFormData.evaluateOrg }}
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="状态：">
                          {{ ['草稿','审核中','已审核'][basicFormData.state] }}
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-row>
                    <el-form-item label="备注："
                      ><el-input
                        type="textarea"
                        :rows="2"
                        placeholder="请输入内容"
                        v-model="basicFormData.remark"
                      >
                      </el-input
                    ></el-form-item>
                    </el-row>
                     <el-row>
                      <el-col :span="12">
                        <el-form-item label="经办人："
                          >{{ basicFormData.evaluateOrg }}
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="最后修改时间：">
                          {{ basicFormData.state }}
                        </el-form-item>
                      </el-col>
                    </el-row>
                  </el-form>
                </div>
              </div>
            </div>
          </div>
          <!-- 分包商汇总 -->
          <SummaryDetails/>
             <!-- 年度动态惩罚查询表 -->
                    <div id="punishmentCon" class="con">
                        <div class="tabs-title" id="punishment">
                            年度动态惩罚查询表
                        </div>
                       <DynamicPenalty/>
                    </div>
          <!-- 评价情况统计表 -->
          <EvaluationStatistics/>
          <!-- 附件信息 -->
          <div id="accessoryCon" class="con">
            <div class="tabs-title" id="accessory">附件信息</div>
            <div class="e-table">
              <div class="top">
                <div class="left">
                  <el-button size="mini">添加</el-button>
                  <!-- <el-button size="mini">删除</el-button> -->
                </div>
              </div>
              <el-table
                :data="tableData"
                border
                style="width: 100%"
                ref="table"
                class="table"
                :max-height="$store.state.tableHeight"
                @cell-mouse-enter="cellMouseEnter"
                @cell-mouse-leave="cellMouseLeave"
                :cell-class-name="cellClsNm"
                :header-cell-style="{ textAlign: 'center' }"
                :cell-style="{ textAlign: 'center' }"
                v-sortTable="{ 'tableData': tableData, '_this': this, 'ref': 'table' }"
              >
                <el-table-column type="selection" width="55"> </el-table-column>
                <el-table-column prop="a1" label="序号" width="100">
                </el-table-column>
                <el-table-column prop="a" label="文件名称" min-width="200">
                </el-table-column>
                <el-table-column prop="b" label="文件大小(kb)" width="200">
                </el-table-column>
                <el-table-column prop="c" label="上传时间" width="200">
                </el-table-column>
                <el-table-column prop="d" label="上传人" width="200">
                </el-table-column>
              </el-table>
            </div>
          </div>
        </div>
      </el-tabs>
    </div>
    <div id="seat" :style="{ height: seatHeight + 'px' }"></div>
    <div class="buttons">
      <span class="tip"
        ><span style="color: red; margin-right: 0">*</span>为必填项</span
      >
      <el-button type="primary" size="small" style="background: #2e61d7"
        >审核</el-button
      >
      <el-button type="primary" size="small" style="background: #2e61d7"
        >撤回</el-button
      >
       <el-button type="primary" size="small" style="background: #2e61d7"
        >作废</el-button
      >
      <el-button
        type="primary"
        size="small"
        style="background: #2e61d7"
        @click="submit"
        >保存并提交</el-button
      >
      <el-button
        type="primary"
        size="small"
        style="background: #2e61d7"
        @click="save"
        >保存</el-button
      >
     <el-button type="danger" size="small" @click="ctDelete">删除</el-button>

      <el-button size="small" @click="handleClose">取消</el-button>
    </div>
      <span style="display: none">{{ localPopConfirm }}</span>
  </div>
</template>

<script>
import '@/utils/jquery.scrollTo.min'
import { mapMutations, mapState } from 'vuex'
import { showPromptPop, showSuccessPop } from '@/utils/common'
// 分包商汇总明细
import SummaryDetails from '@/pages/OutsourcingartyAdmin/subcontractorAdmin/subcontractorEvaluate/collectEvaluate/summaryComponent/summaryDetails'
// 年度动态惩罚查询表
import DynamicPenalty from '@/pages/OutsourcingartyAdmin/subcontractorAdmin/subcontractorEvaluate/collectEvaluate/summaryComponent/dynamicPenalty'
// 评价情况统计表
import EvaluationStatistics from '@/pages/OutsourcingartyAdmin/subcontractorAdmin/subcontractorEvaluate/collectEvaluate/summaryComponent/evaluationStatistics'
export default {
    components: {
        SummaryDetails,
        DynamicPenalty,
        EvaluationStatistics
    },
    data () {
        return {
            tabsName: 'baseInfo',
            form: {
                name: '',
                region: '',
                date1: '',
                date2: '',
                delivery: false,
                type: [],
                resource: '',
                desc: '',
            },
            tableData: [],
            punishmentData: [], //年度惩罚表

            value: '',
            textarea: '',
            screenWidth: 0,
            screenHeight: 0,
            lastConHeight: 0,
            clickTabFlag: false, // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            //基本信息表单数据
            basicFormData: {
                billDate: new Date().toLocaleDateString().replaceAll('/', '-'), //汇总日期
                summartDate: '', //汇总时间区
                evaluateOrg: '', //评价机构
                state: '', //状态
                superiorsOrg: '', //上级机构
                remark: '', //备注
            },
            id: '',
            isSkip: false, //是否跳转
        }
    },
    computed: {
        ...mapState({
            options: state => state.contract.ctClassify,
            popConfirm: 'popConfirm',
            popStatus: 'popStatus',
        }),
        localPopConfirm () {
            if (this.popConfirm) {
                switch (this.popStatus) {
                case 'prompt':
                    this.delData()
                    break
                case 'success':
                    if (this.isSkip) this.handleClose()
                    break
                }
                this.setPopConfirm(false)
            }
            return this.popConfirm
        },
        // 填补底部空白，以使高度够滚动
        seatHeight () {
            return this.screenHeight - 72 - this.lastConHeight
        },
    },
    created () {

        this.getPunishment()
        this.id = this.$route.query.id
        this.getBasicData() //进入页面就获取基本信息数据
    },
    mounted () {
    // 获取最后一个内容区域的高度，计算底部空白
        this.getLastConHeight()

        // 保存所有tabName
        const arr = [
            'baseInfo',
            'subcontractorCollect',
            'punishment',
            'orgCollect',
            'accessory',
        ]
        let $idsTop = []
        window.addEventListener('scroll', () => {
            // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            if (this.clickTabFlag) {
                return
            }

            if (!$idsTop[$idsTop.length - 1]) {
                $idsTop = arr.map(item => {
                    const $item = document.getElementById(item)
                    let itemTop = null
                    if ($item) {
                        itemTop = $item.offsetTop
                    }
                    return itemTop
                })
            }
            const scrollTop =
        document.documentElement.scrollTop || document.body.scrollTop
            // 倒序查找
            let curLocal = 0
            for (let len = $idsTop.length, i = len - 1; i >= 0; i--) {
                let item = $idsTop[i]
                if (scrollTop + 1 >= item) {
                    curLocal = i
                    break
                }
            }
            // 设置对应tabName
            this.tabsName = arr[curLocal]
        })

        this.screenWidth = document.documentElement.clientWidth - this.topHeight
        this.screenHeight = document.documentElement.clientHeight - this.topHeight
        window.addEventListener('resize', () => {
            this.screenWidth = document.documentElement.clientWidth
            this.screenHeight = document.documentElement.clientHeight
            $idsTop = arr.map(item => {
                const itemTop = document.getElementById(item).offsetTop
                return itemTop
            })
        })
    },
    methods: {
        ...mapMutations(['setPopConfirm']),
        //获取基本信息数据
        getBasicData () {

            this.$Ajax
                .httpGet({
                    url: '/outer/summary/findById',
                    params: {
                        id: this.id,
                    },
                })
                .then(res => {
                    this.basicFormData = res
                    this.subDtlList = res.subDtlList
                    console.log('6666', res.subDtlList)
                })
        },
        //分包商汇总列表
        articulars (dtlId) {
            console.log(dtlId)
        },
        //获取年度动态惩罚表
        getPunishment () {
            this.$Ajax
                .httpGet({
                    url: '/outer/rewardsorpunishment/annualPunish',
                    params: {
                        timeInterval: '2008-01-01——2022-01-01',
                    },
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                })
                .then(res => {
                    this.punishmentData = res
                    console.log('?>>>>>', this.getPunishment)
                })
        },
        // 获取最后一个内容区域的高度，计算底部空白
        getLastConHeight () {
            let si = setInterval(() => {
                // 因为dom异步加载，通过轮询找到渲染后的dom，获取高度
                if (document.getElementById('evaluateCon')) {
                    const lastConHeight =
            document.getElementById('evaluateCon').offsetHeight
                    console.log('lastConHeight: ', lastConHeight)
                    this.lastConHeight = lastConHeight
                    console.log('this.lastConHeight', this.lastConHeight)
                    clearInterval(si)
                    si = null
                }
            }, 100)
        },
        ctDelete () {
            showPromptPop('确定要删除吗？删除的数据可以在【回收站】还原或彻底删除。')
        },
        //删除
        delData () {
            this.$Ajax
                .httpGet({
                    url: '/outer/summary/delete',
                    params: {
                        id: this.id,
                    },
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                })
                .then(res => {
                    if (res.code == 200) {
                        showSuccessPop('删除成功！')
                        this.isSkip = true
                    }
                    this.$router.push('/collectEvaluate')
                })
        },
        //保存
        save () {
            // //日期转换格式
            let collectDate = this.getTime(this.basicFormData.billDate)
            this.$Ajax
                .httpPost({
                    url: '/outer/summary/update',
                    params: {
                        billDate: collectDate, //	单据时间
                        billNo: '', //	评价编号
                        closeDate: '', //	截止评价日期
                        evaluateYear: '', //	评价年度
                        gmtCreate: '', //	录入时间
                        gmtModified: '', //最后修改时间
                        groupDtlList: [], //集团汇总明细
                        id: this.id, //评价ID
                        isGroup: 0, //	是否是集团汇总(是集团传1 是分子公司传0)
                        orgId: this.orgId, //	创建机构ID
                        orgName: this.orgName, //	机构名称
                        releasestate: 0, //	发布状态0未发布1发布
                        remark: '', //	备注
                        state: parseInt(this.basicFormData.state), //状态
                        subDtlList: [], //分子公司汇总明细
                        summartDate: '2000-01-01——2022-01-01', //	汇总时间区间
                        workId: '',	//作业ID

                    }
                })
                .then(() => {
                    showSuccessPop('保存成功！')
                    this.isSkip = false

                })
        },
        //提交
        submit () {
            // //日期转换格式
            let collectDate = this.getTime(this.basicFormData.billDate)
            this.$Ajax
                .httpPost({
                    url: '/outer/summary/update',
                    params: {
                        billDate: collectDate, //	单据时间
                        billNo: '', //	评价编号
                        closeDate: '', //	截止评价日期
                        evaluateYear: '', //	评价年度
                        gmtCreate: '', //	录入时间
                        gmtModified: '', //最后修改时间
                        groupDtlList: [], //集团汇总明细
                        id: this.id, //评价ID
                        isGroup: 0, //	是否是集团汇总(是集团传1 是分子公司传0)
                        orgId: this.orgId, //	创建机构ID
                        orgName: this.orgName, //	机构名称
                        releasestate: 0, //	发布状态0未发布1发布
                        remark: '', //	备注
                        state: parseInt(this.basicFormData.state), //状态
                        subDtlList: [], //分子公司汇总明细
                        summartDate: '2000-01-01——2022-01-01', //	汇总时间区间
                        workId: '',	//作业ID

                    }
                })
                .then(() => {
                    showSuccessPop('保存成功！')
                    this.isSkip = false
                    this.$router.push('/collectEvaluate')
                })
        },
        handleFullScreen () {
            this.fullscreen = !this.fullscreen
        },
        //取消
        handleClose () {
            this.$router.push('./collectEvaluate')
        },
        cellClsNm ({ column }) {
            if (column.label === '') {
                return 'fixed-left'
            }
        },
        cellMouseEnter (row) {
            this.MouseEnterId = row.id
        },
        cellMouseLeave () {
            this.MouseEnterId = 0
        },
        onChangeTab (e) {
            console.log('clickTabFlag: ', this.clickTabFlag)
            $.scrollTo(`#${e.name}`, 500)
            // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            this.clickTabFlag = true
            // 动画结束后，恢复状态
            setTimeout(() => {
                this.clickTabFlag = false
            }, 600)
        },
        //日期转换格式
        getTime (dt) {
            var year = dt.getFullYear() //年
            var month = dt.getMonth() + 1 //月
            var date = dt.getDate() //日
            month = month < 10 ? '0' + month : month
            date  = date < 10 ? '0' + date : date
            var str = year + '-' + month + '-' + date
            return str
        },
    },
}
</script>

<style lang="scss" scoped>
.buttons {
  left: 0;
}
.e-form {
  margin-bottom: 80px;
  padding: 0 20px;
  .tabs-title::before {
    content: "";
    height: 22px;
    width: 8px;
    border-radius: 40px;
    background-color: #2e61d7;
    display: block;
    position: absolute;
    left: 20px;
    margin-right: 20px;
  }
}

.e-table {
  background: #fff;
}
// ::v-deep.el-table__body-wrapper::-webkit-scrollbar{
//      width: 6px !important; // 横向滚动条
//     height: 10px !important// 纵向滚动条
// }

::v-deep .el-table__body-wrapper::-webkit-scrollbar {
  width: 6px; // 横向滚动条
  height: 10px; // 纵向滚动条
}
// 滚动条的滑块
::v-deep .el-table__body-wrapper::-webkit-scrollbar-thumb {
  background-color: #ddd;
  border-radius: 5px;
}

.el-tab-pane {
  margin-left: -10px;
}
::v-deep.el-upload-dragger {
  height: 90px;
  margin-left: 300px;
}
</style>